<?PHP

require_once('QuickAddPopup.php');
require_once('ProgramStatusPopUp.php');
require_once "arr_multisort.php";
//get programs


 $sql = "SELECT military, title,  endTimeStamp, source, source_other, type, type_other, ID, active FROM $programTitles_Table WHERE ".strtolower(date("D",$_SESSION['log_timestamp']))." = 'true' AND (startTimeStamp = '-1' OR startTimeStamp <= '".$_SESSION['log_timestamp']."') AND (endTimeStamp = '-1' OR endTimeStamp >= '".$_SESSION['log_timestamp']."') ";
$programs = $db->getAll($sql,DB_FETCHMODE_ASSOC);
if (DB::isError($programs)) {
die ($programs->getMessage());
}



//get underwriters
$sql = "SELECT underwriter, military, spotID, length, type, live FROM $grid_Table WHERE ".strtolower(date("D",$_SESSION['log_timestamp']))." = 'true' AND startTimeStamp <= '".mktime (0,0,0,date("m",$_SESSION['log_timestamp']),date("d",$_SESSION['log_timestamp']),date("Y",$_SESSION['log_timestamp']))."' AND (endTimeStamp = 'NA' OR endTimeStamp >= '".mktime (0,0,0,date("m",$_SESSION['log_timestamp']),date("d",$_SESSION['log_timestamp']),date("Y",$_SESSION['log_timestamp']))."') ";
$spots = $db->getAll($sql,DB_FETCHMODE_ASSOC);
if (DB::isError($spots)) {
	die ($spots->getMessage());
} 

$_SESSION['merged_array'] = array_merge_recursive ($programs, $spots);

$srt = new arr_multisort();
//Set the array to be sorted
$srt->setArray($_SESSION['merged_array']); 
  $srt->addColumn("military",SRT_ASC);
  $srt->addColumn("type",SRT_DESC,"myCompare");
  
  function myCompare($a,$b){
  //echo $a ." # ".$b;
	  if($a==$b){
		return 0;
	  }elseif($a=='NS'){
		return 1;
	  }elseif($a!='NS'){
		return -1;
	  }
  }
  $_SESSION['merged_array'] = $srt->sort();


?>  


<TABLE width="100%" border="0" cellpadding="4" cellspacing="0" class="mainTableBGColor">
	<TR><TD valign="top" align="center">
	
	
		<TABLE border="1" width="100%" cellpadding="2" cellspacing="0">
			<TR class="blockHeader"> <TD></TD> <TD>Air Time</TD> <TD>Program</TD> <TD>length</TD> <TD>Source</TD> <TD>Type</TD> <TD width="1%">Status</TD> </TR>


	<?PHP
	


function description($type){ //write a description in front of PSA's and SPA's
	if($type == 'PSA'){
		return "PSA: ";
	}elseif($type == 'EPSA'){
		return "EPSA: ";
	}elseif($type == 'SPA'){
		return "SPA: ";
	}elseif($type == 'UW'){
		return "UW CREDIT: "; 
	}
}


//QUICK DELETE ITEMS
echo"
<script language=\"JavaScript\">
<!--
function deleteItem(pointer,anchor){
	var form = 'document.' + pointer + '.submit();';
	if(confirm(\"Are you sure you want to delete this item?\")){
		eval(form);
	}

}


function updateItem(pointer,ID,anchor){
	var form = 'index.php?ID=1&fromLog=true&subMenu=0&action=modifyPrograms&programID='+ID+'&anchor='+anchor;
	self.location = form;
}



//-->
</script> ";

 


function options($type,$ID,$pointer,$endTime,$title,$time,$status){///provide update and delet buttons for temporary programs
	if($endTime != '-1'){
	
 	     return "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"formElementTextLeft\">\n
				<form name='quickDelete$pointer' id='quickDelete$pointer' action='formaction.php' method='post'>
				<TR><TD valign=\"top\" align=\"left\"><div id='update$ID'><input name='anchor' type='hidden' value='$pointer'></div><INPUT name=\"radioGroup$pointer\" type=\"radio\" value=\"update:$ID\" onClick=\"updateItem('quickDelete$pointer','$ID','$pointer');\"></TD><TD valign=\"middle\" align=\"left\">update</TD></TR>\n
				<TR><TD valign=\"top\" align=\"left\"><input name=\"deleteID\" type=\"hidden\" value=\"$ID\"><input name=\"action\" type=\"hidden\" value=\"quick_delete\"><INPUT name=\"radioGroup$pointer\" type=\"radio\" value=\"delete:$ID\" onClick=\"deleteItem('quickDelete$pointer','$pointer');\"></TD> <TD valign=\"middle\" align=\"left\">delete</TD></TR>\n
				</form></TABLE>\n ";
	}else{//permanent programs
		//return "<input name=\"activeID$pointer\" type=\"checkbox\" value=\"".$ID."\" checked onClick='showStatus()'>active";
		if($status == 'false'){
			$checked = '';
		}else{
			$checked = 'checked';
		}
		
			    return "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"formElementTextLeft\">\n
				<form name='quickDelete$pointer' id='quickDelete$pointer' action='formaction.php' method='post'>
				<TR><TD valign=\"top\" align=\"left\"><div id='update$ID'><input name='anchor' type='hidden' value='$pointer'></div><INPUT name=\"radioGroup$pointer\" type=\"radio\" value=\"update:$ID\" onClick=\"updateItem('quickDelete$pointer','$ID','$pointer');\"></TD><TD valign=\"middle\" align=\"left\">update</TD></TR>\n
				<TR><TD valign=\"top\" align=\"left\"><input name=\"activeID$pointer\" type=\"checkbox\" value=\"".$ID."\" $checked onClick=\"return !showPopup('ProgramStatus',event,'program',this.name,this.form,'$title','$time','$ID','$pointer')\"></TD> <TD valign=\"middle\" align=\"left\">active</TD></TR>\n
				</form></TABLE>\n ";
		
	}
}
 



function highightRow($currentRow){
	if(isset($_SESSION['anchor']) && $currentRow == $_SESSION['anchor']){
		return "bgcolor='#FFCC99'"; 
	}elseif($currentRow & 1){
		return "bgcolor='#cccccc'"; 
	}else{
		return "bgcolor='#ffffff'"; 
	}
}




	for ($i = 0; $i < count($_SESSION['merged_array']); $i++) {
		
			if($_SESSION['merged_array'][$i]['source'] == 'Other'){
				if($_SESSION['merged_array'][$i]['source_other'] == ""){
					$source = 'Other';
				}else{
					$source = $_SESSION['merged_array'][$i]['source_other'];
				}
			}else{
				$source = $_SESSION['merged_array'][$i]['source'];
			}
			
			
			if($_SESSION['merged_array'][$i]['type'] == 'Other'){
				if($_SESSION['merged_array'][$i]['type_other'] == ""){
					$type = 'Other';
				}else{
					$type = $_SESSION['merged_array'][$i]['type_other'];
				}
			}else{
				$type = $_SESSION['merged_array'][$i]['type']; 
			}
	
	
			if($_SESSION['merged_array'][$i]['ID']){//we have a program
				if($i & 1){
					echo("<TR id='$i' ".highightRow($i)." onmouseover=\"setPointer(this, $i, 'over', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#CCCCCC', '#CCFFCC', '#FFCC99');\"> <TD ".highightRow($i)." class=\"numbers\"><a name='$i'>" .($i+1) .")</a></TD> <TD class=\"blockContent1\" nowrap>" . date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)) ."</TD> <TD class=\"blockContent1\" onClick=\"return !showPopup('spotInfo', event,'".date("g",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 3, 2))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 6, 2))."','".date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','$i')\" ><img src='images/pointer.gif' style='padding-right:4px;'> " . description($type) . stripslashes($_SESSION['merged_array'][$i]['title']) ."</TD> <TD class=\"blockContent1\">&nbsp;</td> <TD class=\"blockContent1\">$source</td> <TD  class=\"blockContent1\">$type</td> 
					<TD  nowrap class=\"formElementText\" >".options($type,$_SESSION['merged_array'][$i]['ID'],$i,$_SESSION['merged_array'][$i]['endTimeStamp'],$_SESSION['merged_array'][$i]['title'],date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)),$_SESSION['merged_array'][$i]['active'])."</TD></TR>\n");	
					}else{
					echo("<TR id='$i' ".highightRow($i)." onmouseover=\"setPointer(this, $i, 'over', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#FFFFFF', '#CCFFCC', '#FFCC99');\"> <TD ".highightRow($i)." class=\"numbers\"><a name='$i'>" .($i+1) .")</a></TD> <TD class=\"blockContent1\" nowrap>" . date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)) ."</TD> <TD class=\"blockContent1\"  onClick=\"return !showPopup('spotInfo', event,'".date("g",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 3, 2))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 6, 2))."','".date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','$i')\" ><img src='images/pointer.gif' style='padding-right:4px;'> " . description($type) . stripslashes($_SESSION['merged_array'][$i]['title']) ."</TD> <TD class=\"blockContent1\">&nbsp;</td> <TD class=\"blockContent1\">$source</td> <TD class=\"blockContent1\">$type</td> 
					<TD nowrap class=\"formElementText\" >".options($type,$_SESSION['merged_array'][$i]['ID'],$i,$_SESSION['merged_array'][$i]['endTimeStamp'],$_SESSION['merged_array'][$i]['title'],date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)),$_SESSION['merged_array'][$i]['active'])."</TD></TR>\n");	
				}	
			
			
			
			}else{//we have a underwriter spot 
				if($_SESSION['merged_array'][$i]['live'] == 'true'){
						$source = 'Live';
					if(isset($incude_UW) && $incude_UW == 'true'){
						$live = " - LIVE<div style='padding-left:14px; font-weight:400;'>&#8226; ".stripslashes($_SESSION['merged_array'][$i]['underwriter'])."</div>";
					}else{
						$live = " - LIVE";
					}
				}else{
						$source = 'AV';
					if(isset($incude_UW) && $incude_UW == 'true'){
						$live = "<div style='padding-left:14px; font-weight:400;'>&#8226; ".stripslashes($_SESSION['merged_array'][$i]['underwriter'])."</div>";
					}else{
						$live = "";
					}
				}
				
					if($i & 1){ 
					echo("<TR id='$i' ".highightRow($i)." onmouseover=\"setPointer(this, $i, 'over', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#CCCCCC', '#CCFFCC', '#FFCC99');\"> <TD ".highightRow($i)." class=\"numbers\"><a name='$i'>" .($i+1) .")</a></TD> <TD class=\"blockContent1\" nowrap>" . date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)) ."</TD> <TD class=\"blockContent1\"  onClick=\"return !showPopup('spotInfo', event,'".date("g",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 3, 2))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 6, 2))."','".date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','$i')\"><img src='images/pointer.gif' style='padding-right:4px;'> ".description($_SESSION['merged_array'][$i]['type']) . stripslashes($_SESSION['merged_array'][$i]['spotID']) ."$live</TD> <TD class=\"blockContent1\">00:" . str_pad(round($_SESSION['merged_array'][$i]['length']), 2, "0", STR_PAD_LEFT) ."</td> <TD class=\"blockContent1\">$source</td> <TD class=\"blockContent1\">".$_SESSION['merged_array'][$i]['type']."</td> <TD nowrap class=\"formElementTextLeft\">&nbsp;</TD></TR>\n");	
					}else{
					echo("<TR id='$i' ".highightRow($i)." onmouseover=\"setPointer(this, $i, 'over', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#FFFFFF', '#CCFFCC', '#FFCC99');\"> <TD ".highightRow($i)." class=\"numbers\"><a name='$i'>" .($i+1) .")</a></TD> <TD class=\"blockContent1\" nowrap>" . date("h",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)).":". substr($_SESSION['merged_array'][$i]['military'], 3, 5)  . " " .date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0)) ."</TD> <TD class=\"blockContent1\"  onClick=\"return !showPopup('spotInfo', event,'".date("g",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 3, 2))."','".number_format(substr($_SESSION['merged_array'][$i]['military'], 6, 2))."','".date("a",mktime ((str_replace (":", ",", $_SESSION['merged_array'][$i]['military'])),0,0,0))."','$i')\"><img src='images/pointer.gif' style='padding-right:4px;'> ".description($_SESSION['merged_array'][$i]['type']) . stripslashes($_SESSION['merged_array'][$i]['spotID']) ."$live</TD> <TD class=\"blockContent1\">00:" .str_pad(round($_SESSION['merged_array'][$i]['length']), 2, "0", STR_PAD_LEFT)  ."</td> <TD class=\"blockContent1\">$source</td> <TD class=\"blockContent1\">".$_SESSION['merged_array'][$i]['type']."</td> <TD nowrap class=\"formElementTextLeft\">&nbsp;</TD></TR>\n");	
					}	
			
			}//end else
	
	
	}//end loop
		
		unset($_SESSION['anchor']);
	
?>
		
			
				
</TABLE>
	</TD></TR>
</TABLE>